#ifndef QFILEPROPERTYEDITOR_H
#define QFILEPROPERTYEDITOR_H

#include "QtPropertyExplorerLib.h"

#include <QWidget>
#include <QToolButton>
#include <QLineEdit>
#include <QMimeData>

namespace QPE{


class QTPROPERTYEXPLORER_DLL_EXPORT QFileEditWidget : public QWidget
{
	Q_OBJECT

public:
	QFileEditWidget(QWidget* parent, bool is_directory = false);
	virtual ~QFileEditWidget();


	//! This function returns the file currently set to this editor
	QString getFile();

	//! Needed for proper event handling
	bool eventFilter(QObject* obj, QEvent* ev);

	//! Returns the directory/file setting
	//! \return True, if a directory dialog is being shown, false if a file dialog
	bool isDirectory();

signals:
	//! This signal is emitted when the user changed the curret file.
	//! Actions triggering this signal are either using the file dialog
	//! to select a new file or changing the file path in the line edit.
	void dataChangedByUser(const QString &getFile, QFileEditWidget* editor);

	//! This signal is emitted whenever dataChangedByUser() gets emmitted
	//! and is connected to the delegate's commitData() signal
	void commitData(QWidget* editor);


public slots:
	//! Sets the current file, does not check if it is valid
	//! \param file The new filepath the widget should show
	//! \emit_signal If true, this will emit the dataChangedByUser()-signal (if file differs from the current file)
	void setFile(const QString &getFile, bool emit_signal = false);

	//! Sets a filter for the file field
	//! \param dialog_filter The filter used for the File Dialog
	//! \param filter_list The list of file endings. The filters are being checked using regular expressions
	void setFilter(const QString& dialog_filter = QString(), const QStringList& filter_list = QStringList());

	//! Sets whether the property stores a directory or a file
	void setDirectory(bool dir);

private slots:
	//! This slot gets activated, when the "..." button gets clicked
	void onToolButtonClicked();

protected:
	void dragEnterEvent(QDragEnterEvent* event);
	void dragMoveEvent(QDragMoveEvent* event);
	void dragLeaveEvent(QDragLeaveEvent* event);
	void dropEvent(QDropEvent* event);



	//! This function checks the mime data, if it is compatible with the filters
	virtual bool checkMimeData(const QMimeData* data, QString& getFile) const;

	//! This checks, if a file is compatible with the filters
	virtual bool checkFileFilter(const QString& getFile) const;


	QString CurrentFilePath;
	QToolButton* ToolButton;
	QLineEdit* FileLineEdit;
	QString FileDialogFilter;
	QStringList FilterList;

	//! Specifies whether it is being looked for a directory (true) or a file (false, default)
	bool Directory;
};

}

#endif // QFILEPROPERTYEDITOR_H
